package com.lucy.nepu.module.tmaterial.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.nepu.module.tmaterial.lib.TMaterialDict;
import com.lucy.nepu.module.tmaterial.lib.TMaterialDictService;
import com.lucy.nepu.module.tmaterial.lib.impl.TMaterialDictCreateParams;
import com.lucy.nepu.module.tmaterial.lib.impl.TMaterialDictQueryParams;
import com.lucy.nepu.module.tmaterial.lib.impl.TMaterialDictUpdateParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("nepu/tmaterial")
@Api(tags = "教材词典")
public class TMaterialLibController {
    @Autowired
    TMaterialDictService service;

    @PostMapping("dict")
    @ApiOperation(value = "词典创建",notes = "")
    public ResponseDTO<Object> dictCreate(@RequestBody @Validated TMaterialDictCreateParams params){
        TMaterialDict o = new TMaterialDict();
        BeanUtil.copyProperties(params,o, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.saveOne(o)
        );
    }

    @DeleteMapping("dict/{id}")
    @ApiOperation(value = "词典删除",notes = "")
    public ResponseDTO<Object> dictDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("dict")
    @ApiOperation(value = "词典更新",notes = "")
    public ResponseDTO<Object> dictUpdate(@RequestBody @Validated TMaterialDictUpdateParams params){
        Optional<TMaterialDict> dictOp = service.getOne(params.getId());
        if (!dictOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        TMaterialDict o = dictOp.get();
        BeanUtil.copyProperties(params,o,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.updateOne(o)
        );
    }

    @GetMapping("dict")
    @ApiOperation(value = "词典列表")
    public ResponseDTO<Object> dictListQuery(TMaterialDictQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        TMaterialDict o = new TMaterialDict();
        BeanUtil.copyProperties(params,o,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<TMaterialDict> example = Example.of(o);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }


    @GetMapping("dict/{id}")
    @ApiOperation(value = "词典查询",notes = "")
    public ResponseDTO<Object> dictQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                service.getOne(id)
        );
    }
}
